#!/bin/bash

FROM_CODE=big5
TO_CODE=utf8

[ $# -lt 1 ] && {
echo "$(basename $0) from_dir [to_dir]"
exit 1
}

FDIR="$1"
TDIR="${2:-out}"

mkdir -p "$TDIR"

function need_convert()
{
	[[ "$1" =~ \.html$ ]]
}

find "$FDIR" | while read -r line ; do
	if [ -d "$line" ] ; then
		mkdir -p "$TDIR/$line"
		continue
	fi
	if need_convert "$line" ; then
		echo convert $line
		iconv -f $FROM_CODE -t $TO_CODE -o "$TDIR/$line" "$line"
		continue
	fi

	echo copy $line
	cp -f "$line" "$TDIR/$line"
done
